﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Teradata.Client.Provider;
using System.Configuration;
using System.Text;
using System.Data;

public partial class _getChartData : System.Web.UI.Page
{
    protected String ViewName = String.Empty;
    protected String ColumnName = String.Empty;
    protected String[] Months = { "Joe", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };
    protected String Series1 = String.Empty, Series2 = String.Empty, Series3 = String.Empty;
    protected String ConnString = ConfigurationManager.ConnectionStrings["EDW_GROUP_ID"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        DateTime startTime = DateTime.Now; Trace.Write(System.Reflection.MethodInfo.GetCurrentMethod().Name + " Started");
        Response.ContentType = "text/xml";

        if (Request.QueryString["PRIORMONTH"] == null)
            getChartData(Request.QueryString["SQLFILTER"], Request.QueryString["OFFSET"]);
        else if (Request.QueryString["PRIORMONTH"] == String.Empty)
            getChartData(Request.QueryString["SQLFILTER"], Request.QueryString["OFFSET"]);
        else
            getChartData(Request.QueryString["SQLFILTER"], Request.QueryString["OFFSET"], Request.QueryString["PRIORMONTH"]);

        Response.Write(getHeader() + Series1 + Series2 + Series3 + getFooter());

        DateTime stopTime = DateTime.Now; TimeSpan duration = stopTime - startTime; Trace.Write(System.Reflection.MethodInfo.GetCurrentMethod().Name + " Ended: " + duration.ToString());
    }

    protected string getHeader()
    {
        RollupMetadata meta = (RollupMetadata)Session["RollupMetadata"];
        string sHeight = (meta.DisplayDaily() ? "480" : meta.DisplayBoth() ? "300" : "0");
        string sWidth = (meta.DisplayDaily() ? "800" : meta.DisplayBoth() ? "500" : "0");

        string sTemplate = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath("templates\\Chart1Header.xaml"));
        return String.Format(sTemplate, sWidth, sHeight, meta.DailyLabel);

    }

    protected string getFooter()
    {
        return " </vc:Chart.Series> " +
               "</vc:Chart>";
    }

    protected void getChartData(String SQLFILTER, String OFFSET)
    {
        DateTime startTime = DateTime.Now; Trace.Write(System.Reflection.MethodInfo.GetCurrentMethod().Name + " Started");
        RollupMetadata meta = (RollupMetadata)Session["RollupMetadata"];
        StringBuilder sb1 = new StringBuilder("  <vc:DataSeries RenderAs=\"Line\" AxisYType=\"Secondary\" LegendText=\"HSIA\" >  <vc:DataSeries.DataPoints> ");
        StringBuilder sb2 = new StringBuilder("  <vc:DataSeries RenderAs=\"Line\" AxisYType=\"Secondary\" LegendText=\"IPTV\" >  <vc:DataSeries.DataPoints> ");
        StringBuilder sb3 = new StringBuilder("  <vc:DataSeries RenderAs=\"Line\" AxisYType=\"Secondary\" LegendText=\"VOIP\" >  <vc:DataSeries.DataPoints> ");

        string ROLLUPTYPE = SQLFILTER.Split('=')[0].Trim();
        if (SQLFILTER.Contains(" IS NULL")) ROLLUPTYPE = SQLFILTER.Split(' ')[0].Trim();

        if (ROLLUPTYPE != null)
        {
            DataView dv = (new RolledUpBiz(ROLLUPTYPE)).DailyChartData(SQLFILTER, OFFSET);

            for (int i = 0; i < dv.Count; i++)
            {

                sb1.Append(String.Format("    <vc:DataPoint AxisXLabel=\"{1} {0}\" YValue=\"{2}\" />",
                    dv[i].Row["DY"], Months[(int)dv[i].Row["MN"]], dv[i].Row["HSIA_NET"]));

                sb2.Append(String.Format("    <vc:DataPoint AxisXLabel=\"{1} {0}\" YValue=\"{2}\" />",
                    dv[i].Row["DY"],
                    Months[(int)dv[i].Row["MN"]],
                    dv[i].Row["IPTV_NET"]));

                sb3.Append(String.Format("    <vc:DataPoint AxisXLabel=\"{1} {0}\" YValue=\"{2}\" />",
                    dv[i].Row["DY"], Months[(int)dv[i].Row["MN"]], dv[i].Row["VOIP_NET"]));
            }
        }

        sb1.Append("   </vc:DataSeries.DataPoints>   </vc:DataSeries> ");
        sb2.Append("   </vc:DataSeries.DataPoints>   </vc:DataSeries> ");
        sb3.Append("   </vc:DataSeries.DataPoints>   </vc:DataSeries> ");

        Series1 = sb1.ToString();
        Series2 = sb2.ToString();
        Series3 = sb3.ToString();

        DateTime stopTime = DateTime.Now; TimeSpan duration = stopTime - startTime; Trace.Write(System.Reflection.MethodInfo.GetCurrentMethod().Name + " Ended: " + duration.ToString());
    }

    //Get chart data for Pacing Chart
    protected void getChartData(String SQLFILTER, String OFFSET, String PriorMonthSQL)
    {
        DateTime startTime = DateTime.Now; Trace.Write(System.Reflection.MethodInfo.GetCurrentMethod().Name + " Started");

        StringBuilder sb1 = new StringBuilder("  <vc:DataSeries RenderAs=\"Area\" AxisYType=\"Secondary\" LegendText=\"Pacing\" >  <vc:DataSeries.DataPoints> ");
        StringBuilder sb2 = new StringBuilder("  <vc:DataSeries RenderAs=\"Line\" AxisYType=\"Secondary\" LegendText=\"Total\" >  <vc:DataSeries.DataPoints> ");

        string ROLLUPTYPE = SQLFILTER.Split('=')[0].Trim();

        if (ROLLUPTYPE != null)
        {
            RolledUpBiz RollUp = new RolledUpBiz(ROLLUPTYPE);
            DataView dv = (RollUp).DailyChartData(SQLFILTER, OFFSET);

            DataView dvMonthPrior = (RollUp).MonthlyRaw(PriorMonthSQL);

            decimal dPriorMonthTot = 0;
            if (dvMonthPrior != null && dvMonthPrior.Count>0 && dvMonthPrior[0] != null)
                dPriorMonthTot = Convert.ToDecimal(dvMonthPrior[0]["TOT_NET"]);

            decimal dPerDiem = dPriorMonthTot / Convert.ToDecimal(DateTime.DaysInMonth(DateTime.Now.AddMonths(-Convert.ToInt16(OFFSET)).Year, DateTime.Now.AddMonths(-Convert.ToInt16(OFFSET)).Month));

            for (int i = 0; i < dv.Count; i++)
            {
                sb1.Append(String.Format("    <vc:DataPoint AxisXLabel=\"{1} {0}\" YValue=\"{2}\" />",
                    dv[i].Row["DY"], Months[(int)dv[i].Row["MN"]], (int)dv[i].Row["DY"] * dPerDiem));

                sb2.Append(String.Format("    <vc:DataPoint AxisXLabel=\"{1} {0}\" YValue=\"{2}\" />",
                    dv[i].Row["DY"],
                    Months[(int)dv[i].Row["MN"]],
                    (int)dv[i].Row["IPTV_NET"] + (int)dv[i].Row["HSIA_NET"] + (int)dv[i].Row["VOIP_NET"]));

            }
        }

        sb1.Append("   </vc:DataSeries.DataPoints>   </vc:DataSeries> ");
        sb2.Append("   </vc:DataSeries.DataPoints>   </vc:DataSeries> ");

        Series1 = sb1.ToString();
        Series2 = sb2.ToString();

        DateTime stopTime = DateTime.Now; TimeSpan duration = stopTime - startTime; Trace.Write(System.Reflection.MethodInfo.GetCurrentMethod().Name + " Ended: " + duration.ToString());
    }



}